package car.manager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

//I'll use inheritance to manage DataBase interaction
//each class will manage a different table
//good idea? I don't think so but I don't see a better one without using cool plug-ins that I don't understand
public class DataBaseConnection extends SQLiteOpenHelper {

	public static final int VERSION = 1;

	// Tables names
	public static final String DB_NAME = "car_manager.sqlite";
	public static final String PROFILE_TABLE = "profile";
	public static final String GARAGE_TABLE = "garage";
	public static final String CAR_TABLE = "car";

	// PROFILE TABLE
	public static final String PROFILE_ID = "id";
	public static final String PROFILE_NAME = "name";
	public static final String PROFILE_CREDIT = "credit";
	public static final String PROFILE_EXPERIENCE = "experience";

	// GRAGE TABLE
	public static final String GARAGE_ID = "id";
	public static final String GARAGE_NAME = "name";
	public static final String GARAGE_CAPACITYT = "capacity";
	public static final String GARAGE_DATE_OF_CREATION = "date_of_creation";
	public static final String GARAGE_NUMBER_OF_CARS = "date_of_creation";

	// CAR TABLE
	public static final String CAR_ID = "id";
	public static final String CAR_BRAND = "brand";
	public static final String CAR_MODEL = "model";
	public static final String CAR_LICENSING_PLATE = "icense_plate";
	public static final String CAR_DATE_OF_CREATION = "date_of_creation";
	public static final String CAR_COLOR = "color";
	public static final String CAR_CONDITION = "condition";
	public static final String CAR_MILAEGE = "mileage";
	public static final String CAR_MARKET_VALUE = "market_value";
	public static final String CAR_DISPLAY_FOTO = "display_foto";
	public static final String CAR_SECUNDARY_FOTO1 = "foto1";
	public static final String CAR_SECUNDARY_FOTO2 = "foto2";
	public static final String CAR_SECUNDARY_FOTO3 = "foto3";

	public DataBaseConnection(Context context) {
		super(context, DB_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		createTable(db);
	}

	private void createTable(SQLiteDatabase db) {

		db.execSQL("create table " + PROFILE_TABLE + " (" + PROFILE_ID
				+ " integer primary key autoincrement not null," + PROFILE_NAME
				+ " text," + PROFILE_CREDIT + " float," + PROFILE_EXPERIENCE
				+ " float" +

				");");
	}

	//can this be abstract? that would mean all children have to implenet
	//maybe with a more abstract name it's ok
//	public LinkedList<UserProfile> getAllProfiles(){
//		
//		
//	}
	
	
	
	// this method exists with the purpose obtaining the most recent User
	// Profile from the database
	// this implies that ids are managed by the database
	// right now ids are being managed on the business Layer which may not be a
	// great idea
	public int getLastUserProfileId() {

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(PROFILE_TABLE, null, null, null, null, null,
				null, null);

		// if Profile table is not empty
		if (cursor.moveToFirst()) {
			int numberOfContacts = cursor.getCount();
			int[] idList = new int[numberOfContacts];
			int iterator = 0;

			do {
				int columnIndex = cursor.getColumnIndex(PROFILE_ID);
				idList[iterator] = cursor.getInt(columnIndex);

			} while (cursor.moveToNext());

			int maxId = getMaxId(idList);
			return maxId;
		}
		return -1;
	}

	private int getMaxId(int[] idList) {

		if (idList.length != 0) {
			int maxId = idList[0];
			for (int i = 0; i < idList.length; i++) {
				if (idList[i] > maxId) {
					maxId = idList[i];
				}
			}
			return maxId;
		}
		return -1;
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		db.execSQL("DROP TABLE IF EXISTS " + PROFILE_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + GARAGE_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + CAR_TABLE);

		// Create tables again
		onCreate(db);
	}

}
